var Registry = {};
var Core = {};

Core.Tools = {
	redirect: function (url, blank) {
		if (blank) {
			window.open(url, '_blank');
		} else {
			location.href = url;
		}
	}
}

Core.Site = {
	changeLanguage: function (lang_code) {
		var url = Registry.public_url;
		url += lang_code + '/';
		url += Registry.current_path;
		if ($.trim(location.hash)) {
			url += location.hash;
		}
		location.href = url;
	}
}

Core.Schema = {
	init: function () {
		var hash = window.location.hash;
		if ($.trim(hash)) {
			hash = hash.replace(/^\#+/, '');
			Core.Schema.openTab(hash);
		}
		
		//$(".schema_date").datepicker({dateFormat: 'yy-mm-dd'});

		var config = {
			// General options
			script_url: Registry.public_url + 'ext/tiny_mce/tiny_mce.js',
			mode: 'textareas',
			theme: 'advanced',
			skin: 'default',
			height: '400px',
			plugins: 'table,advimage,advlink,inlinepopups,preview,media,contextmenu,paste,directionality,fullscreen',

			// Theme options
			theme_advanced_buttons1: 'newdocument,preview,fullscreen,code,|,cut,copy,paste,pastetext,pasteword,|,bold,italic,underline,strikethrough,sub,sup,|,formatselect,fontselect,fontsizeselect',
			theme_advanced_buttons2: 'image,media,|,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,ltr,rtl,|,styleprops,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,|,hr,removeformat',
			theme_advanced_buttons3: 'tablecontrols,|,visualaid',

			theme_advanced_toolbar_location: 'top',
			theme_advanced_statusbar_location: 'bottom',
			theme_advanced_resizing: true,

			//document_base_url: Registry.public_url,
			convert_urls: false,
			//relative_urls: false,
			//remove_script_host: false,

			// Directionality
			theme_advanced_toolbar_align: 'left',

			file_browser_callback: 'Core.Schema.tinyMceFileBrowserOpen'
		}

		var rtl_config = {
			directionality: 'rtl',
			//language: 'he',
			theme_advanced_toolbar_align: 'right'
		}

		$('textarea.schema_html').tinymce(config);
		$('textarea.schema_html_rtl').tinymce($.extend({}, config, rtl_config));
	},
	
	tinyMceFileBrowserOpen: function (field_name, url, type, win) {
		var browserUrl = Registry.base_url + 'file/browsertinymce';
		tinyMCE.activeEditor.windowManager.open({
			file : browserUrl,
			title : 'File browser',
			width : 960,
			height : 700,
			resizable : 'yes',
			inline : 'yes',
			close_previous : 'no',
			popup_css : false
		}, {
			window : win,
			input : field_name
		});
		return false;
	},
	
	fileBrowserOpen: function (prefix, simple) {
		var browserUrl = Registry.base_url + 'file/browser';
		$('#file_browser_content').attr('src', browserUrl);
		$('#file_browser').attr('prefix', prefix).attr('simple', simple).modal();
	},

	fileBrowserResult: function (obj) {
		var prefix = $('div#file_browser').modal('hide').attr('prefix');
		var simple = parseInt($('div#file_browser').attr('simple'));
		$('div#file_browser_content').attr('src', 'about:blank');
		if (simple) {
			$('#' + prefix).val(obj.name);
		} else {
			$('#' + prefix).val(obj.id);
			$('#' + prefix + '_preview').attr('src', obj.icon).attr('title', obj.name_orig);
			$('#' + prefix + '_link').attr('href', obj.url);
			$('#' + prefix + '_select').hide();
			$('#' + prefix + '_info').show();
		}
	},

	removeFile: function (prefix) {
		$('#' + prefix).val(0);
		$('#' + prefix + '_preview').attr('src', '').attr('title', '');
		$('#' + prefix + '_link').attr('href', '#');
		$('#' + prefix + '_select').show();
		$('#' + prefix + '_info').hide();
		return false;
	},
	
	previewFile: function (id) {
		var field_obj = $('#' + id);
		if ($.trim(field_obj.val())) {
			Core.Tools.redirect(Registry.upload_url + $.trim(field_obj.val()), 1);
		} else {
			alert('No file selected');
		}
	},
	
	changeLanguage: function (lang_code) {
		var url = Registry.base_url + 'schema/language/' + lang_code + '/';
		if (/\?/.test(location.href)) {
			url += '&';
		} else {
			url += '?';
		}
		url += 'url=' + escape(location.href);
		location.href = url;
	},

	openTab: function (id) {
		$('div.schema_blocks > div.schema_block').hide();
		$('#schema_block_' + id).show();

		$('div.schema_tabs li.active').removeClass('active');
		$('#schema_tab_' + id).addClass('active');
	}	
}

Core.List = {
	openInfo: function (id) {
		$('.list_item_info_block').hide();
		$('.list_item_list_block').show();
		$('#list-' + id).hide();
		$('#info-' + id).show();
	},
	

	closeInfo: function () {
		$('.list_item_info_block').hide();
		$('.list_item_list_block').show();
	}
}